Imports System.Data.OleDb
Imports ComponentFactory.Krypton.Toolkit

Public Class Pagamento

    Private valor As Double
    Private codOS As Integer
    Private ds As DataSet
    Private da As OleDbDataAdapter
    Private conexao As OleDbConnection

    Sub New(ByVal valor As Double, ByVal codOS As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        Me.valor = valor
        Me.codOS = codOS

        conexao = FabricaDeConexoes.CriarConexao()
        carregarFormasPagamento()

        lblValor.Text = String.Format("Valor R$ {0:0.00}", valor)
    End Sub

    Private Sub carregarFormasPagamento()
        conexao.Open()

        Try
            ds = New DataSet()
            da = New OleDbDataAdapter("SELECT * FROM tblTipoPagamento", conexao)
            da.Fill(ds, "tblTipoPagamento")
            cbTipoPagamento.DataSource = ds.Tables("tblTipoPagamento")
            cbTipoPagamento.DisplayMember = "descricaoTipoPagamento"
            cbTipoPagamento.ValueMember = "codTipoPagamento"

        Catch ex As Exception
            Debug.WriteLine(ex)
        Finally
            conexao.Close()
        End Try
    End Sub

    Private Sub btnConcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConcluir.Click
        Dim resp = KryptonMessageBox.Show("Confirma o pagamento?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If (resp = Windows.Forms.DialogResult.Yes) Then
            conexao.Open()
            Dim tipo = DirectCast(cbTipoPagamento.SelectedItem, DataRowView).Item("codTipoPagamento")
            Dim cmd As New OleDbCommand(String.Format("INSERT INTO tblPagamento(codOrdemServico, dataPagamento, codTipoPagamento, valorPagamento) " & _
                                                      "VALUES ({0}, {1}, {2}, {3})", codOS, Date.Now.ToString("dd/MM/yyyy"), tipo, valor), conexao)
            cmd.ExecuteNonQuery()
            If (tipo = 1) Then
                cmd.CommandText = String.Format("INSERT INTO tblEntradaCaixa (valorentradaCaixa, codOrdemServico, motivoEntradaCaixa, dtEntradaCaixa) " & _
                                                "VALUES ({0}, {1}, '{2}', '{3}')", valor, codOS, "Pagamento OS", Date.Now.ToString("dd/MM/yyyy"))
                cmd.ExecuteNonQuery()
            End If

            cmd.CommandText = "SELECT SUM(valorPagamento) AS soma FROM tblPagamento WHERE codOrdemServico = " & codOS
            Dim dr = cmd.ExecuteReader()
            dr.Read()
            Dim soma = dr("soma")
            dr.Close()
            cmd.CommandText = "SELECT valorTotalOrdemServico FROM tblOrdemServico WHERE codOrdemServico = " & codOS
            dr = cmd.ExecuteReader()
            dr.Read()
            Dim preco = dr("valorTotalOrdemServico")
            dr.Close()
            If soma >= preco Then
                cmd.CommandText = "UPDATE tblOrdemServico SET statusOrdemServico = 'Paga' WHERE codOrdemServico = " & codOS
                cmd.ExecuteNonQuery()
            End If

            KryptonMessageBox.Show("Pagamento efetuado com sucesso.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.Dispose()
            conexao.Close()
        End If

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        KryptonMessageBox.Show("Pagamento cancelado.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.Dispose()
    End Sub
End Class